<template>
  <el-dialog title="编辑分组" width="500px">
    <div class="group-edit-container">
      <el-table :data="groups" border style="width: 100%">
        <el-table-column label="分组名称" prop="name" width="200">
          <template #default="scope">
            <ButtonInput v-model="scope.row.name" @update:modelValue="handleGroupNameChange(scope.row.groupId, $event)"/>
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="desc" />
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="scope">
            <el-button type="danger" link @click="deleteGroup(scope.row.groupId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="add-button-container">
        <el-button type="primary" @click="showAddGroupDialog = true">添加分组</el-button>
      </div>
    </div>

    <!-- 添加分组弹窗 -->
    <el-dialog v-model="showAddGroupDialog" title="添加分组" width="400px">
      <el-form label-width="80px">
        <el-form-item label="分组名称">
          <el-input v-model="newGroupName" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="newGroupDesc" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAddGroupDialog = false">取消</el-button>
        <el-button type="primary" @click="createNewGroup">确认</el-button>
      </template>
    </el-dialog>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, toRaw, watch } from 'vue';
import { useTextStore } from '@/stores/config/configTextStore';
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus';
const electron = require('electron');

const showAddGroupDialog = ref(false);
const newGroupName = ref('');
const newGroupDesc = ref('');
const textStore = useTextStore();
const groups = ref(textStore.groups);

// 监听分组数据变化
watch(
  () => textStore.groups,
  (newGroups) => {
    groups.value = newGroups;
  },
  { deep: true }
);

// 处理分组名称修改
const handleGroupNameChange = (groupId: number, newName: string) => {
  const group = groups.value.find(g => g.groupId === groupId);
  if (group) {
    group.name = newName;
    // 保存更改
    electron.ipcRenderer.invoke('saveTextStore', toRaw(textStore.$state));
  }
};

// 删除分组
const deleteGroup = (groupId: number) => {
  ElMessageBox.confirm(
    '确认删除该分组吗？',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(() => {
      const index = groups.value.findIndex(g => g.groupId === groupId);
      if (index !== -1) {
        // 移除该分组
        groups.value.splice(index, 1);
        // 保存更改
        electron.ipcRenderer.invoke('saveTextStore', toRaw(textStore.$state));
      }
      ElMessage({
        type: 'success',
        message: '成功删除分组',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消删除分组',
      })
    });
};

// 创建新分组
const createNewGroup = () => {
  if (!newGroupName.value.trim()) {
    ElNotification({title: '错误', message: '分组名称不能为空', type: 'error', duration: 3000});
    return;
  }

  // 调用store的添加分组方法
  textStore.createGroup(newGroupName.value, newGroupDesc.value);

  // 重置表单并关闭弹窗
  newGroupName.value = '';
  newGroupDesc.value = '';
  showAddGroupDialog.value = false;

};
</script>

<style scoped>
.group-edit-container {
  padding: 10px 0;
}

.add-button-container {
  margin-top: 15px;
  text-align: right;
}
</style>